package controller;



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Animal;
import model.Field;
import model.Fox;
import model.Wolf;
import model.Rabbit;
import model.Hunter;
import model.Simulator;
import controller.JTextFieldNumbersOnly;

/**
 * The class SettingsController extends AbstractController
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */

public class SettingsController extends AbstractController implements ActionListener{
    
	private JTextField grassFoodValueRabbitField;
	private JTextField breedingAgeRabbitField;
	private JTextField maxAgeRabbitField;
	private JTextField breedingProbabilityRabbitField;
	private JTextField maxLitterSizeRabbitField;
	private JTextField breedingAgeFoxField;
	private JTextField maxAgeFoxField;
	private JTextField breedingProbabilityFoxField, maxLitterSizeFoxField, rabbitFoodValueFoxField, breedingAgeWolfField, maxAgeWolfField, breedingProbabilityWolfField, maxLitterSizeWolfField, foxFoodValueWolfField;
    private JButton saveButton;
	private JTextField foxPercField;
	private JTextField rabbitPercField;
	private JTextField wolfPercField;
	private JTextField hunterPercField;
	private JTextField grassPercField;
	private JTextField roadWidthField;
	private JTextField carsYField;
	private JSlider fullnessSlider;
	private JTextField ziekteIncubatieField;
	private JTextField ziekteInfectionField;
	private JTextField grassInfectionField;
	private JTextField ziekteGenField;
	private JCheckBox road;
	
	
	
	/**
	 * Create a new SettingsController using the simulator
	 * @param simulator
	 */
    public SettingsController(Simulator simulator) {
        super(simulator);
        breedingAgeRabbitField = new JTextFieldNumbersOnly("6");
        maxAgeRabbitField = new JTextFieldNumbersOnly("40");
        breedingProbabilityRabbitField = new JTextFieldNumbersOnly("0.4");
        maxLitterSizeRabbitField = new JTextFieldNumbersOnly("4");
        grassFoodValueRabbitField = new JTextFieldNumbersOnly("10");
        
        breedingAgeFoxField = new JTextFieldNumbersOnly("10");
        maxAgeFoxField = new JTextFieldNumbersOnly("150");
        breedingProbabilityFoxField = new JTextFieldNumbersOnly("0.35");
        maxLitterSizeFoxField = new JTextFieldNumbersOnly("5");
        rabbitFoodValueFoxField = new JTextFieldNumbersOnly("7");
        
        breedingAgeWolfField = new JTextFieldNumbersOnly("12");
        maxAgeWolfField = new JTextFieldNumbersOnly("160");
        breedingProbabilityWolfField = new JTextFieldNumbersOnly("0.2");
        maxLitterSizeWolfField = new JTextFieldNumbersOnly("4");
        foxFoodValueWolfField = new JTextFieldNumbersOnly("8");
        
        ziekteIncubatieField = new JTextFieldNumbersOnly("5");
        ziekteInfectionField = new JTextFieldNumbersOnly("0.1");
        grassInfectionField = new JTextFieldNumbersOnly("0.1");
        ziekteGenField = new JTextFieldNumbersOnly("0.9");
        
        foxPercField = new JTextFieldNumbersOnly("12");
	    rabbitPercField = new JTextFieldNumbersOnly("20");
        wolfPercField = new JTextFieldNumbersOnly("4");
		
		JPanel rabbits = new JPanel();
		rabbits.setBorder(new EmptyBorder(6,6,6,6));
	    rabbits.setLayout( new GridLayout(0, 1) );
	    
	    rabbits.add(new JLabel("Rabbits"));
	    rabbits.add(new JLabel("Breeding Age: "));
	    rabbits.add(breedingAgeRabbitField);
	    
	    rabbits.add(new JLabel("Max Age: "));
	    rabbits.add(maxAgeRabbitField);
	    
	    rabbits.add(new JLabel("Breeding Probalility: "));
	    rabbits.add(breedingProbabilityRabbitField);
	    
	    rabbits.add(new JLabel("Litter size: "));
	    rabbits.add(maxLitterSizeRabbitField);
	    
	    rabbits.add(new JLabel("Grass Food Value"));
	    rabbits.add(grassFoodValueRabbitField);
	    
	    rabbits.add(new JLabel("Rabbit %: "));
	    rabbits.add(rabbitPercField);
	    
	    rabbits.add(new JLabel("Viral Strain:"));
	    rabbits.add(new JLabel("Grass Infection Chance: "));
	    rabbits.add(grassInfectionField);
	    
	    rabbits.add(new JLabel("Airborne Infection Chance: "));
	    rabbits.add(ziekteInfectionField);
	    
	    rabbits.add(new JLabel("Virus Incubation: "));
	    rabbits.add(ziekteIncubatieField);
	    
	    rabbits.add(new JLabel("Virus susceptabillity gene chance:"));
	    rabbits.add(ziekteGenField);
	    
	    
	    JPanel foxes = new JPanel();
		foxes.setBorder(new EmptyBorder(6,6,6,6));
	    foxes.setLayout( new GridLayout(0, 1) );
	    
	    foxes.add(new JLabel("Foxes"));
	    foxes.add(new JLabel("Breeding Age: "));
	    foxes.add(breedingAgeFoxField);
	    
	    foxes.add(new JLabel("Max Age: "));
	    foxes.add(maxAgeFoxField);
	    
	    foxes.add(new JLabel("Breeding Probalility: "));
	    foxes.add(breedingProbabilityFoxField);
	    
	    foxes.add(new JLabel("Litter size: "));
	    foxes.add(maxLitterSizeFoxField);
	    
	    foxes.add(new JLabel("Rabbit Food Value: "));
	    foxes.add(rabbitFoodValueFoxField);
	    
	    foxes.add(new JLabel("Field"));
	    foxes.add(new JLabel("Fox %: "));
	    foxes.add(foxPercField);
	    
	    
	    JPanel wolves = new JPanel();
		wolves.setBorder(new EmptyBorder(6,6,6,6));
	    wolves.setLayout( new GridLayout(0, 1) );
	    
	    wolves.add(new JLabel("Wolves"));
	    wolves.add(new JLabel("Breeding Age: "));
	    wolves.add(breedingAgeWolfField);
	    
	    wolves.add(new JLabel("Max Age: "));
	    wolves.add(maxAgeWolfField);
	    
	    wolves.add(new JLabel("Breeding Probalility: "));
	    wolves.add(breedingProbabilityWolfField);
	    
	    wolves.add(new JLabel("Litter size: "));
	    wolves.add(maxLitterSizeWolfField);
	    
	    wolves.add(new JLabel("Fox Food Value: "));
	    wolves.add(foxFoodValueWolfField);
	    
	    wolves.add(new JLabel("Wolf %: "));
	    wolves.add(wolfPercField);
	    
	    
        hunterPercField = new JTextFieldNumbersOnly("1");
        grassPercField = new JTextFieldNumbersOnly("8");
        roadWidthField = new JTextFieldNumbersOnly("4");
        carsYField = new JTextFieldNumbersOnly("4");
        fullnessSlider = new JSlider(JSlider.HORIZONTAL, 1, 10, 5);  
        road = new JCheckBox();
        
        
	    JPanel field = new JPanel();
		field.setBorder(new EmptyBorder(6,6,6,6));
		field.setLayout( new GridLayout(0, 1) );

	    
	    field.add(new JLabel("Hunter %: "));
	    field.add(hunterPercField);
	    
	    field.add(new JLabel("Grass %: "));
	    field.add(grassPercField);
	    
	    field.add(new JLabel("Road Width: "));
	    field.add(roadWidthField);
	    
	    field.add(new JLabel("Cars per lane: "));
	    field.add(carsYField);
	    
	    field.add(new JLabel("Fieldfullness: "));
	    field.add(fullnessSlider);
	    
	    field.add(new JLabel("Road?"));
	    field.add(road);
	    
	    
	    saveButton=new JButton("Save");
	    this.add(saveButton);
	    saveButton.addActionListener(this);
	    
	    this.add(rabbits);
	    this.add(foxes);
	    this.add(wolves);
	    this.add(field);
        
        setVisible(true);
		
		
    }
    
    /**
     * overrides the actionPerformed method
     * @param e
     * @override
     */
	 public void actionPerformed(ActionEvent e)
	 {
	 	if (e.getSource()==saveButton){
	 			//Rabbits
	   			int breedingAgeRab = Integer.parseInt(breedingAgeRabbitField.getText());
	       		int maxAgeRab = Integer.parseInt(maxAgeRabbitField.getText());
	       		double breedingProbabilityRab = Double.parseDouble(breedingProbabilityRabbitField.getText());
	       		int maxLitterSizeRab = Integer.parseInt(maxLitterSizeRabbitField.getText());
	   			int grassFoodValueRab = Integer.parseInt(grassFoodValueRabbitField.getText());
	        		
	        	//Foxes
	        	int breedingAgeFox = Integer.parseInt(breedingAgeFoxField.getText());
	        	int maxAgeFo = Integer.parseInt(maxAgeFoxField.getText());
	        	double breedingProbabilityFox = Double.parseDouble(breedingProbabilityFoxField.getText());
	        	int maxLitterSizeFox = Integer.parseInt(maxLitterSizeFoxField.getText());
	        	int rabbitFoodValueFox = Integer.parseInt(rabbitFoodValueFoxField.getText());
	        		
	        	//Wolves
	        	int breedingAgeWol = Integer.parseInt(breedingAgeWolfField.getText());
	        	int maxAgeWol = Integer.parseInt(maxAgeWolfField.getText());
	        	double breedingProbabilityWol = Double.parseDouble(breedingProbabilityWolfField.getText());
	        	int maxLitterSizeWol = Integer.parseInt(maxLitterSizeWolfField.getText());
	        	int foxFoodValueWol = Integer.parseInt(foxFoodValueWolfField.getText());
	        	
	        	//Field
	        	int foxPerc = Integer.parseInt(foxPercField.getText());
	        	int rabbitPerc = Integer.parseInt(rabbitPercField.getText());
	        	int wolfPerc = Integer.parseInt(wolfPercField.getText());
	        	int hunterPerc = Integer.parseInt(hunterPercField.getText());
	        	int grassPerc = Integer.parseInt(grassPercField.getText());
	        	int roadWidth = Integer.parseInt(roadWidthField.getText());
	        	int carsY = Integer.parseInt(carsYField.getText());
	        	int fullness = fullnessSlider.getValue();
	        		
	        	//Virus
	        		
	        	int incubation = Integer.parseInt(ziekteIncubatieField.getText());
	        	double hunterInfection = Double.parseDouble(ziekteInfectionField.getText());
	        	double grassInfection = Double.parseDouble(grassInfectionField.getText());
	        		
	        	if(road.isSelected()) {
	        		simulator.setRoad(true);
	        	}
	        	else {
	        		simulator.setRoad(false);
	        	}
	        		
	        	simulator.setProbabilities(foxPerc, rabbitPerc, wolfPerc, hunterPerc, grassPerc);
	        	simulator.setRoadSettings(roadWidth, carsY);
	        	simulator.setFullness(fullness);
	        	
        		Rabbit.setBreedingAge(breedingAgeRab);
        		Rabbit.setMaxAge(maxAgeRab);
        		Rabbit.setBreedingProbability(breedingProbabilityRab);
        		Rabbit.setMaxLitterSize(maxLitterSizeRab);
        		Rabbit.setGrassFoodValue(grassFoodValueRab);
        		Rabbit.setGrassInfection(grassInfection);
        		
        		Fox.setBreedingAge(breedingAgeFox);
        		Fox.setMaxAge(maxAgeFo);
        		Fox.setBreedingProbability(breedingProbabilityFox);
        		Fox.setMaxLitterSize(maxLitterSizeFox);   
        		Fox.setRabbitFoodValue(rabbitFoodValueFox);
        		
        		Wolf.setBreedingAge(breedingAgeWol);
        		Wolf.setMaxAge(maxAgeWol);
        		Wolf.setBreedingProbability(breedingProbabilityWol);
        		Wolf.setMaxLitterSize(maxLitterSizeWol);
        		Wolf.setFoxFoodValue(foxFoodValueWol);
        		
        		Animal.setIncubation(incubation);
        		Animal.setInfection(hunterInfection);
        		
        		simulator.setReady();
	        	}
	    }
}